import pymysql
#配置连接池的包
from mysql.connector.cursor import MySQLCursorBufferedDict
from mysql.connector.pooling import MySQLConnectionPool
from conf.config import poolCon

pool = None

# 获取连接池管理对象
def get_mysql_connection_pool():
    global pool
    # 如果连接池管理对象已被初始化，直接返回此对象
    if pool:
        return pool
    # 否则初始化连接池管理对象，并返回
    pool = MySQLConnectionPool(15,**poolCon)
    return pool

class MysqlClient:
    # 初始化函数中获取连接池管理对象
    def __init__(self):
        self.connection_pool = get_mysql_connection_pool()

    def fetchall(self,sql):
        # 获取一个连接
        con = self.connection_pool.get_connection()
        cur = con.cursor(cursor_class=MySQLCursorBufferedDict)
        # 调用cursor中封装的函数，必须先execute，再调用fetch。
        cur.execute(sql)
        r = cur.fetchall()
        cur.close()
        con.close()
        return r
    def execute(self,sql):
        con = self.connection_pool.get_connection()
        cur = con.cursor(cursor_class=MySQLCursorBufferedDict)
        cur.execute(sql)
        cur.close()
        con.close()

cli = MysqlClient()


if __name__ == '__main__':
    print("...")